package dk.hewison.client.album;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import dk.hewison.client.Log;
import dk.hewison.client.domain.Photo;
import dk.hewison.client.panels.CompositeView;
import dk.hewison.client.panels.PhotoHolder;

import java.util.List;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-09 12:39:39 +0000 (Mon, 09 Feb 2009) $:
 * @version $Revision: 363 $:
 */
public class PhotoSheet extends CompositeView<List<Photo>> {

    ScrollPanel scrollPanel;
    List<Photo> photos;
    FlowPanel flowPanel;
    //private NEffectPanel effectPanel;

    int imageCount = 0;

    int loadedCount = 0;

    long start;

    public PhotoSheet() {
    }

    public List<Photo> getModel() {
        return photos;
    }

    public void initView() {
        /* Fade fadeIn = new Fade();
        fadeIn.getProperties().setStartOpacity(0);
        fadeIn.getProperties().setEndOpacity(100);*/
        //effectPanel = new NEffectPanel();
        //effectPanel.addEffect(fadeIn);
        flowPanel = new FlowPanel();
        flowPanel.setWidth("100%");


        scrollPanel = new ScrollPanel(flowPanel);

        scrollPanel.setStyleName("photosheet");

        initWidget(scrollPanel);
        //setStyleName("albumthumbnail");
        //effectPanel.add(panel);
        setStyleName("photosheet");
    }

    public void setModel(List<Photo> photos) {

        start = System.currentTimeMillis();
        this.photos = photos;

        redraw();

        /*Iterator<Widget> iterator = panel.iterator();
        while (iterator.hasNext()) {
            PhotoListHolder photoListHolder = (PhotoListHolder) iterator.next();
            photoListHolder.loadThumbnails();
        }*/


    }


    private void redraw() {
        flowPanel.clear();
        if (photos == null) return;
        imageCount = photos.size();

        for (Photo directory : photos) {
            flowPanel.add(createPhotoHolder(directory));
        }
    }

    private Widget createPhotoHolder(Photo photo) {
        return new PhotoHolder(photo);
    }

    public void fadeIn() {
        //effectPanel.playEffects();
    }

    /**
     * Fired when a widget encounters an error while loading.
     *
     * @param sender the widget sending the event.
     */
    public void onError(Widget sender) {

    }

    /**
     * Fired when a widget finishes loading.
     *
     * @param sender the widget sending the event.
     */
    public void onLoad(Widget sender) {
        loadedCount++;
        if (loadedCount == imageCount) {
            long took = System.currentTimeMillis() - start;
            Log.debug("Took " + took + " ms to load " + imageCount + " user.");
            //Window.alert("Took " + took + " ms to load " + imageCount + " user.");
        }
    }

    public Widget getMyWidget() {
        return flowPanel;
    }

    public void resize() {
        int width = Window.getClientWidth() - 15;
        int height = Window.getClientHeight() - getAbsoluteTop();
        scrollPanel.setPixelSize(width, height);
    }
}
